﻿using Controls.Views;
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Reflection.Emit;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace MCUPic.Views.PicGroup
{
    public partial class PicGroup : UserControl
    {
        public event EventHandler PicturePathsChanged;
        

        private class PicItem
        {
            public string Path { get; set; }
            public PicItemView PicView { get; set;}

            public PicItem(string path,PicItemView picItemView)
            {
                this.Path = path;
                this.PicView = picItemView;
            }
        }

        public IEnumerable<string> PicturePaths { get { return _items.Select(i => i.Path); }  }

        private IList<PicItem> _items;
        public string SelectedItem { get; private set; }

        [Browsable(true)]
        [EditorBrowsable(EditorBrowsableState.Never)]
        [Bindable(false)]
        [DesignerSerializationVisibility(DesignerSerializationVisibility.Hidden)]
        public override string Text
        {
            get
            {
                return base.Text;
            }
            set
            {
                base.Text = value;
            }
        }


        public PicGroup()
        {
            InitializeComponent();
            this.AllowDrop = true;
            this.DragDrop += PicGroup_DragDrop;
            this.DragOver += PicGroup_DragOver;
            _items = new List<PicItem>();
        }

        private void PicGroup_DragOver(object sender, DragEventArgs e)
        {
            e.Effect = DragDropEffects.Link;
        }

        private void PicGroup_DragDrop(object sender, DragEventArgs e)
        {
            if (e.Data.GetDataPresent(DataFormats.FileDrop))
            {
                string[] paths = ((string[])e.Data.GetData(DataFormats.FileDrop.ToString()));
                
                for (int i = 0; i < paths.Length; i ++)
                {
                    AddItem(paths[i]);
                }
            }
        }

        public void AddItem(string path)
        {
            if (string.IsNullOrEmpty(path))
            {
                throw new ArgumentNullException("path");
            }

            try
            {
                Image image = Image.FromFile(path);
            }
            catch (Exception ex)
            {
                MessBox.Show($"{path}文件不是图片！");
                return;
            }

            PicItemView picItemView = new PicItemView();
            PicItem picItem = new PicItem(path, picItemView);
            picItemView.PathStr = path;
            picItemView.OnRemoved += () =>
            {
                int index = _items.IndexOf(picItem);
                _items.Remove(picItem);
                UpdateItemsLocation(index);
            };

            _items.Add(picItem);
            AddControl(picItemView);
            PicturePathsChanged?.Invoke(this, EventArgs.Empty);
        }

        private void AddControl(PicItemView picItemView)
        {
            int count = grBox_Pic.Controls.Count;
            picItemView.Location = new Point(2, count == 0 ? 20 : 20 + count * (picItemView.Height + 3));
            picItemView.Width = this.grBox_Pic.Width - 4;
            this.grBox_Pic.Controls.Add(picItemView);
            picItemView.MouseDown += (s, e) =>
            {
                SelectedItem = _items.FirstOrDefault(it => it.PicView == s)?.Path;
                this.OnMouseDown(e);
            };
        }

        private void UpdateItemsLocation(int index)
        {
            int count = grBox_Pic.Controls.Count;
            for(int i = index; i < count; i++)
            {
                var item = grBox_Pic.Controls[i];
                item.Location = new Point(2, i == 0 ? 20 : 20 + i * (item.Height + 3));
            }
        }

        protected override void OnLoad(EventArgs e)
        {
            base.OnLoad(e);
            this.grBox_Pic.Size = new System.Drawing.Size(this.Width - 4 , this.Height - 4);
            this.grBox_Pic.Location = new System.Drawing.Point(2, 2);
        }

        protected override void OnTextChanged(EventArgs e)
        {
            base.OnTextChanged(e);
            grBox_Pic.Text = this.Text;
        }
    }
}
